package ModelLayer;
import java.util.ArrayList;

/**
 * Write a description of class PersonCon here.
 * 
 * @author (Frederik, Simon, Claus, Minh, Nichlas & Nicolai) 
 * @version (a version number or a date)
 */
public class PersonCon
{
    // instance variables - replace the example below with your own
    private ArrayList<Person> personer;
    private static PersonCon instance;

    /**
     * Constructor for objects of class PersonCon
     */
    private PersonCon()
    {
        // initialise instance variables
        personer = new ArrayList<Person>();
    }

    public Loan findLoan(String id)
    {
        int i = 0;
        boolean found = false;
        
        while(!found && i<personer.size()) {
            if(personer.get(i).findLoan(id) != null) {
                found = true;
            }
            else {
                i++;
            }
            
            if(found) {
                return personer.get(i).findLoan(id);
            }        
        }
        return null;
    }

    public static PersonCon getInstance()
    {
        if(instance == null)
        {
            instance = new PersonCon();
        }
        return instance;
    }

    /**
     * 
     * @param p er vores angivelse af Person klassen
     */
    public void addPerson(Person p)
    {
        personer.add(p);
    }

    public void deletePerson(Person p)
    {
        personer.remove(p);
    }
    
    /**
     * en metode til at finde en person ud fra "phone"
     * 
     * der startes med while (ikke fundet og i er mindre end personer størrelse)
     * 
     *hvis (phone er lig med (personer <-> phone)
     *så retuneres fundet 
     *ellers incrementeres der med en og søges igen
     *
     *hvis fundet retuneres person
     */
    public Person findPerson(String phone)
    {
        int i = 0;
        boolean found = false;
        while (!found && i<personer.size()){

            if (phone.equals (personer.get(i).getPhone())){
                found = true;
            }
            else {
                i++;
            }

            if (found){                
                return personer.get(i);
            }

        }
        return null;
    }

    public int getPersonsSize()
    {
        return personer.size();
    }

    public void clearPersons()
    {
        personer.clear();
    }
}
